package cn.tedu.tmall.passport.service.impl;

import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.util.JwtUtils;
import cn.tedu.tmall.passport.dao.repository.IUserRepository;
import cn.tedu.tmall.passport.pojo.param.UserLoginParam;
import cn.tedu.tmall.passport.pojo.po.UserPO;
import cn.tedu.tmall.passport.pojo.vo.UserLoginVO;
import cn.tedu.tmall.passport.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UserService implements IUserService {
    @Autowired
    private IUserRepository userRepository;
        private String secretKey = "fdsfads0000dxcfdfds3fsdfdsfdsfdsf";
    @Override
    public UserLoginVO login(UserLoginParam userLoginParam, HttpServletRequest httpServletRequest) {
        //验证用户名
        String username = userLoginParam.getUsername();
        //UserPO userPO = userRepository.getUserInfoByUsername(username);
        UserPO userPO = new UserPO();
        userPO.setEnable(1);
        userPO.setId(1L);
        if(userPO==null) {
            throw new ServiceException(50001,"用户名或密码错误");
        }

        String password = userLoginParam.getPassword();

        if(!userPO.getEnable().equals(1)) {
            throw new ServiceException(50002,"登录失败,账号被禁用");
        }
        Map<String,Object> userData = new HashMap<>();
        userData.put("id",userPO.getId());
        userData.put("name",userPO.getUsername());
        String jwt = JwtUtils.generate(userData,secretKey);
        log.debug("jwt:{}",jwt);
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(1L);
        userLoginVO.setToken(jwt);
        userLoginVO.setUsername("llx");

        return userLoginVO;
    }
}
